#include "geometry.h"
#include "math/vertex.h"
#include "irenderer.h"
#include <memory.h>

namespace gecko
{

	//------------------------------------------------------------------------------------------------

	Geometry::Geometry(void)
		: m_batch(NULL)
	{
	}

	//------------------------------------------------------------------------------------------------

	Geometry::~Geometry(void)
	{
		if ( m_vertices )
		{
			delete [] m_vertices;
			m_vertices = NULL;
		}

		if ( m_indices )
		{
			delete [] m_indices;
			m_indices = NULL;
		}
	}

	//------------------------------------------------------------------------------------------------

	void Geometry::Initialise( Vertex* vertices, short numVertices, unsigned short* indices, short numIndices, IEffect* effect, IVertexBuffer::ePrimitiveType primitiveType )
	{
		m_primitiveType = primitiveType;
		m_effect = effect;
		
		m_numVertices = numVertices;
		m_numIndices = numIndices;

		m_vertices = new Vertex[m_numVertices];
		memcpy(m_vertices, vertices, sizeof(Vertex) * m_numVertices);

		m_indices = new unsigned short[m_numIndices];
		memcpy(m_indices, indices, sizeof(unsigned short) * m_numIndices);
	}

	//------------------------------------------------------------------------------------------------

	IEffect* Geometry::GetEffect()
	{
		return m_effect;
	}

	void Geometry::CreateTriangle( const Vector3f& p1, const Vector3f& p2, const Vector3f& p3, const Colour& colour, IEffect* effect )
	{
		static gecko::Vertex verts[] =
		{
			gecko::Vertex(p1, colour.ToInt(), 0.0f, 0.0f),
			gecko::Vertex(p2, colour.ToInt(), 0.0f, 0.0f),
			gecko::Vertex(p3, colour.ToInt(), 0.0f, 0.0f)
		};

		static unsigned short indices[] =
		{
			0,
			1,
			2
		};

		Initialise(verts, 3, indices, 3, effect, gecko::IVertexBuffer::PT_TRIANGLELIST);
	}

}
